local extension = Package:new("1v1_test_mod")
extension.extensionName = "1v1_test"
extension:loadSkillSkelsByPath("./packages/1v1_test/pkg/1v1_test_mod/skills")

-- 此包的武将只可能出现在1v1_mode 其余模式强制关闭
extension.game_modes_whitelist = { "1v1_mode" }

Fk:loadTranslationTable{
  ["1v1_test_mod"] = "单挑测试改将",
}

--------------------------------------
-- 以下为该拓展包含有的武将列表
--------------------------------------

-- 移动版

Fk:loadTranslationTable{
  ["1v1_mobile"] = "单手杀",
}

-- 谋攻篇
Fk:loadTranslationTable{
  ["1v1_mou"] = "单手杀谋",
}

General:new(extension, "1v1_mou__guanyu", "shu", 4):addSkills { "1v1_mou__wusheng", "mou__yijue" }
Fk:loadTranslationTable{
  ["1v1_mou__guanyu"] = "谋关羽",

  ["~1v1_mou__guanyu"] = "大哥，翼德，来生再于桃园，论豪情壮志……",
}

-- 无前缀
General:new(extension, "1v1_mobile__nanhualaoxian", "qun", 3):addSkills { "1v1_mobile__yufeng", "mobile__tianshu" }
Fk:loadTranslationTable{
  ["1v1_mobile__nanhualaoxian"] = "南华老仙",

  ["~1v1_mobile__nanhualaoxian"] = "天机求而近，执而远……",
  ["!1v1_mobile__nanhualaoxian"] = "纷总总兮九州，何寿夭兮在予？",
}

General:new(extension, "1v1_mobile__pangdegong", "qun", 3):addSkills { "1v1_mobile__pingcai", "yinship" }
Fk:loadTranslationTable{
  ["1v1_mobile__pangdegong"] = "庞德公",

  ["~1v1_mobile__pangdegong"] = "吾知人而不自知，何等荒唐。",
}

General:new(extension, "1v1_mobile__peixiu", "qun", 3):addSkills { "xingtu", "1v1_mobile__juezhi" }
Fk:loadTranslationTable{
  ["1v1_mobile__peixiu"] = "裴秀",

  ["~1v1_mobile__peixiu"] = "既食寒石散，便不可饮冷酒啊……",
}

General:new(extension, "1v1_mobile__zhoufang", "wu", 3):addSkills { "duanfa", "1v1_mobile_sp__youdi" }
Fk:loadTranslationTable{
  ["1v1_mobile__zhoufang"] = "周鲂",

  ["~1v1_mobile__zhoufang"] = "功亏一篑，功亏一篑啊。",
}

-- Online
Fk:loadTranslationTable{
  ["1v1_ol"] = "单OL",
}

-- 界限突破
Fk:loadTranslationTable{
  ["1v1_ol_ex"] = "单OL界",
}

General:new(extension, "1v1_ol_ex__xusheng", "wu", 4):addSkills { "1v1_ol_ex__pojun" }

Fk:loadTranslationTable{
  ["1v1_ol_ex__xusheng"] = "界徐盛",

  ["~1v1_ol_ex__xusheng"] = "盛只恨，不能再为主公，破敌致胜了。",
}

-- 门阀士族
Fk:loadTranslationTable{
  ["1v1_olz"] = "单OL族",
}

General:new(extension, "1v1_olz__wanghun", "jin", 3):addSkills { "fuxun", "1v1_olz__chenya", "zhongliu" }
Fk:loadTranslationTable{
  ["1v1_olz__wanghun"] = "族王浑",

  ["$zhongliu_1v1_olz__wanghun1"] = "国潮汹涌，当为中流之砥柱。",
  ["$zhongliu_1v1_olz__wanghun2"] = "执剑斩巨浪，息风波者出我辈。",
  ["~1v1_olz__wanghun"] = "灭国之功本属我，奈何枉作他人衣……",
}

local wanglun = General:new(extension, "1v1_olz__wanglun", "wei", 3)
wanglun:addSkills { "qiuxin", "1v1_olz__jianyuan", "zhongliu" }
Fk:loadTranslationTable{
  ["1v1_olz__wanglun"] = "族王沦",

  ["$zhongliu_1v1_olz__wanglun1"] = "上善若水，中流而引全局。",
  ["$zhongliu_1v1_olz__wanglun2"] = "泽物无声，此真名士风流。",
  ["~1v1_olz__wanglun"] = "人间多锦绣，奈何我云不喜……",
}

General:new(extension, "1v1_olz__zhonghui", "wei", 3, 4):addSkills { "1v1_olz__yuzhi", "xieshu", "baozu" }
Fk:loadTranslationTable{
  ["1v1_olz__zhonghui"] = "族钟会",

  ["$baozu_1v1_olz__zhonghui1"] = "不为刀下脍，且做俎上刀。",
  ["$baozu_1v1_olz__zhonghui2"] = "吾族恒大，谁敢欺之？",
  ["$baozu_1v1_olz__zhonghui3"] = "动我钟家的人，哼，你长了几个脑袋？",
  ["$baozu_1v1_olz__zhonghui4"] = "有我在一日，谁也动不得吾族分毫。",
  ["$baozu_1v1_olz__zhonghui5"] = "钟门欲屹万年，当先居万人之上。",
  ["$baozu_1v1_olz__zhonghui6"] = "诸位同门，随我钟会赌一遭如何？",
  ["~1v1_olz__zhonghui"] = "谋事在人，成事在天……",
}

General:new(extension, "1v1_olz__zhongyan", "jin", 3, 3, General.Female):addSkills { "guangu", "1v1_olz__xiaoyong", "baozu" }
Fk:loadTranslationTable{
  ["1v1_olz__zhongyan"] = "族钟琰",

  ["$baozu_1v1_olz__zhongyan1"] = "好女宜家，可度大厄。",
  ["$baozu_1v1_olz__zhongyan2"] = "宗族有难，当施以援手。",
  ["~1v1_olz__zhongyan"] = "此间天下人，皆分一斗之才……",
}

General:new(extension, "1v1_olz__zhongyao", "wei", 3):addSkills { "1v1_olz__chengqi", "1v1_olz__jieli", "baozu" }
Fk:loadTranslationTable{
  ["1v1_olz__zhongyao"] = "族钟繇",

  ["$baozu_1v1_olz__zhongyao1"] = "立规定矩，教习钟门之材。",
  ["$baozu_1v1_olz__zhongyao2"] = "放任纨绔，于族是祸非福。",
  ["~1v1_olz__zhongyao"] = "幼子得宠而无忌，恐生无妄之祸……",
}

General:new(extension, "1v1_olz__zhongyu", "wei", 3):addSkills { "1v1_olz__jiejian", "huanghan", "baozu" }
Fk:loadTranslationTable{
  ["1v1_olz__zhongyu"] = "族钟毓",

  ["$baozu_1v1_olz__zhongyu1"] = "弟会腹有恶谋，不可不防。",
  ["$baozu_1v1_olz__zhongyu2"] = "会期大祸将至，请晋公恕之。",
  ["~1v1_olz__zhongyu"] = "百年钟氏，一朝为尘矣……",
}

-- SP
Fk:loadTranslationTable{
  ["1v1_ol_sp"] = "单OLSP",
}

local sp_jiangwei = General:new(extension, "1v1_ol_sp__jiangwei", "wei", 4)
sp_jiangwei:addSkills { "kunfen", "1v1_ol_sp__fengliang" }
sp_jiangwei:addRelatedSkill("ol_ex__tiaoxin")
Fk:loadTranslationTable{
  ["1v1_ol_sp__jiangwei"] = "姜维",

  ["$ol_ex__tiaoxin_1v1_ol_sp__jiangwei1"] = "今日天公作美，怎能不战而退？",
  ["$ol_ex__tiaoxin_1v1_ol_sp__jiangwei2"] = "贼将无胆，何不早降！",
  ["~1v1_ol_sp__jiangwei"] = "伯约已尽力而为，奈何大汉，国运衰微……",
}

-- 无前缀
General:new(extension, "1v1_ol__ruanhui", "wei", 3, 3, General.Female):addSkills { "1v1_ol__mingcha", "jingzhong" }
Fk:loadTranslationTable{
  ["1v1_ol__ruanhui"] = "阮慧",

  ["~1v1_ol__ruanhui"] = "贱妾茕茕守空房，忧来思君不敢忘……",
}

General:new(extension, "1v1_ol__wuyi", "shu", 4):addSkills { "1v1_ol__benxi" }
Fk:loadTranslationTable{
  ["1v1_ol__wuyi"] = "吴懿",

  ["~1v1_ol__wuyi"] = "终有疲惫之时！休矣！",
}

General:new(extension, "1v1_ol__xurong", "qun", 4):addSkills { "1v1_ol__xionghuo", "shajue" }
Fk:loadTranslationTable{
  ["1v1_ol__xurong"] = "徐荣",

  ["~1v1_ol__xurong"] = "此生无悔，心中无愧。",
}

local zhouqun = General:new(extension, "1v1_ol__zhouqun", "shu", 4)
zhouqun:addSkills { "tianhou", "1v1_ol__chenshuo" }
zhouqun:addRelatedSkills { "tianhou_hot", "tianhou_fog", "tianhou_rain", "tianhou_frost"}
Fk:loadTranslationTable{
  ["1v1_ol__zhouqun"] = "周群",

  ["~1v1_ol__zhouqun"] = "知万物而不知己命，大谬也……",
}

General:new(extension, "1v1_ol__zhujun", "qun", 4):addSkills { "1v1_ol__cuipo" }
Fk:loadTranslationTable{
  ["1v1_ol__zhujun"] = "朱儁",

  ["~1v1_ol__zhujun"] = "李郭匹夫，安敢辱我！",
}

--------------------------------------
-- 以下为游戏模式 - 单挑模式定义
--------------------------------------

local desc_1v1_mode = [[
# 单挑模式简介

基本上就是一个二人身份局，但有如下改动：

## 结算上的改动

* 主公无主公技。
* 大部分与身份相关的结算参照军争。
* 包含个别需要特别定制的武将。
* (OL)隐匿系列武将删除隐匿机制。

## 游戏体验上的改动

* 出牌时间固定为1800秒。
* 手气卡固定为0张。
* 不可启用副将。
* 游戏开始前，房主负责分配先后手，并为双方选择武将。
* 若对方为COMP玩家，则房主自动控制对方。
* 可以立刻投降结束游戏。
]]

---@param room Room
local function getOtherPlayers(room, p)
  local ret = table.simpleClone(room.players)
  table.removeOne(ret, p)
  return ret
end

---@param room Room
local function getPlayerByScreenName(room, name)
  return table.find(room.players, function(p)
    return p._splayer:getScreenName() == name
  end)
end

---@param self Room
local function prepareGeneral(self, player, general)
  self:findGeneral(general)
  local g = Fk.generals[general]
  self:setPlayerProperty(player, "general", general)
  self:setPlayerProperty(player, "gender", g.gender)
  self:setPlayerProperty(player, "kingdom", g.kingdom)
end

local mode_1v1_get_logic = function()
  local logic = GameLogic:subclass("1v1_mode_logic")

  function logic:initialize(room)
    GameLogic.initialize(self, room)
    self.room.settings.luckTime = 0
    self.room.timeout = 1800
  end

  function logic:assignRoles()
    local room = self.room
    local cowner = room.room:getOwner()
    local owner = getPlayerByScreenName(room, cowner:getScreenName())
    local opponent = getOtherPlayers(room, owner)[1]

    local choice = room:askToChoice(owner, {
      choices = { "1v1_lord", "1v1_rebel" },
      cancelable = false,
      prompt = "#1v1_mode-choose-role",
      skill_name = "1v1_mode", -- 这玩意怎么不能省略啊
    })

    if choice == "1v1_lord" then
      room:setPlayerProperty(owner, "role", "lord")
      room:setPlayerProperty(opponent, "role", "rebel")
    else
      room:setPlayerProperty(owner, "role", "rebel")
      room:setPlayerProperty(opponent, "role", "lord")
    end

    room:setPlayerProperty(owner, "role_shown", true)
    room:setPlayerProperty(opponent, "role_shown", true)
  end

  function logic:chooseGenerals()
    local room = self.room
    local cowner = room.room:getOwner()
    local owner = getPlayerByScreenName(room, cowner:getScreenName())
    local opponent = getOtherPlayers(room, owner)[1]
    local generalNum = room.settings.generalNum
    local generals = room:getNGenerals(generalNum)

    room:sendLog{
      type = "#1v1_mode-choose-generals",
      toast = true,
    }

    local chosen_generals = room:askToChooseGeneral(owner, {
      generals = generals,
      n = 2,
      prompt = "#1v1_mode-choose-generals",
    })

    prepareGeneral(room, room.players[1], chosen_generals[1])
    prepareGeneral(room, room.players[2], chosen_generals[2])
    room:setCurrent(room.players[1])

    if opponent._splayer:getState() == fk.Player_Robot then
      owner:control(opponent)
    end
    room:askToChooseKingdom(room.players)
  end

  function logic:attachSkillToPlayers()
    local room = self.room

    local addRoleModSkills = function(player, skillName)
      local skill = Fk.skills[skillName]
      if skill:hasTag(Skill.Lord) then
        return
      end

      if skill:hasTag(Skill.AttachedKingdom) and not table.contains(skill:getSkeleton().attached_kingdom, player.kingdom) then
        return
      end

      room:handleAddLoseSkills(player, skillName, nil, false)
    end

    for _, p in ipairs(room.alive_players) do
      local skills = Fk.generals[p.general].skills
      for _, s in ipairs(skills) do
        addRoleModSkills(p, s.name)
      end
      for _, sname in ipairs(Fk.generals[p.general].other_skills) do
        addRoleModSkills(p, sname)
      end
    end

    -- 此处修复不知道什么原因第一次烧条可以确定的bug！
    room:animDelay(1)
  end

  return logic
end

local mode_1v1 = fk.CreateGameMode{
  name = "1v1_mode",
  minPlayer = 2,
  maxPlayer = 2,
  main_mode = "role_mode",
  rule = "#1v1_rule&",
  surrender_func = function(self, playedTime)
    return { { text = "1v1_surrender_hint", passed = true } }
  end,
  logic = mode_1v1_get_logic,
  -- is_counted = Util.TrueFunc,
}

extension:addGameMode(mode_1v1)

Fk:loadTranslationTable{
  ["1v1_mode"] = "单挑模式",
  [":1v1_mode"] = desc_1v1_mode,

  ["1v1_lord"] = "主公（先手）",
  ["1v1_rebel"] = "反贼（后手）",
  ["#1v1_mode-choose-role"] = "请房主选择自己的身份（先后手）",
  ["#1v1_mode-choose-generals"] = "请房主选择两张武将，分别作为先手武将与后手武将",
  ["1v1_surrender_hint"] = "此为单挑模式，打不过可立即投降",
}

return extension
